package org.jetlinks.protocol.official.mqtt.down.zigbeeGateway.handler;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import org.hswebframework.web.exception.BusinessException;
import org.jetlinks.core.message.function.FunctionInvokeMessage;
import org.jetlinks.protocol.official.ObjectMappers;
import org.jetlinks.protocol.official.TopicPayload;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author bingzhong
 * @Date 2024/12/31 13:43
 * @Description zigbee 网关设置mqtt指向
 */
public class ZigbeeGatewayMqttDirectSetting {

    static ObjectMapper mapper = ObjectMappers.JSON_MAPPER;

    @SneakyThrows
    public static TopicPayload encode(FunctionInvokeMessage functionInvokeMessage, String topicPatten) {

        String mqttID = String.valueOf(functionInvokeMessage
                                                .getInput("mqttID")
                                                .orElseThrow(() -> new BusinessException("mqttID 不能为空")));

        String ip = String.valueOf(functionInvokeMessage
                                              .getInput("ip")
                                              .orElseThrow(() -> new BusinessException("ip 不能为空")));

        String port = String.valueOf(functionInvokeMessage
                                              .getInput("port")
                                              .orElseThrow(() -> new BusinessException("port 不能为空")));

        String keepAlive = String.valueOf(functionInvokeMessage
                                              .getInput("keepAlive")
                                              .orElse("60"));

        // example: gateway/in/1873591472619356160
        String subTopic = String.valueOf(functionInvokeMessage
                                              .getInput("subTopic")
                                              .orElseThrow(() -> new BusinessException("subTopic 不能为空")));

        // example: gateway/out/1873591472619356160
        String pubTopic = String.valueOf(functionInvokeMessage
                                              .getInput("pubTopic")
                                              .orElseThrow(() -> new BusinessException("pubTopic 不能为空")));

        String username = String.valueOf(functionInvokeMessage
                                                 .getInput("username")
                                                 .orElse("admin"));

        String password = String.valueOf(functionInvokeMessage
                                                 .getInput("password")
                                                 .orElse("admin"));

        String topic = String.valueOf(functionInvokeMessage
                                              .getInput("topic")
                                              .orElseThrow(() -> new IllegalArgumentException("topic not exists")));



        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("fid", "123");
        dataMap.put("object", "system");
        dataMap.put("method", "mqttConfig");
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("mqttID", mqttID);
        paramMap.put("ip", ip);
        paramMap.put("port", port);
        paramMap.put("keepAlive", keepAlive);
        paramMap.put("subTopic", subTopic);
        paramMap.put("pubTopic", pubTopic);
        paramMap.put("username", username);
        paramMap.put("password", password);
        dataMap.put("param", paramMap);
        byte[] payloadBytes = mapper.writeValueAsBytes(dataMap);

        return TopicPayload.of(topic, payloadBytes);

    }

}
